import PAGE from "../../../utils/middleware/page";
import { storeBindingsBehavior } from "mobx-miniprogram-bindings";
import store from "../../../store/index";
const app = getApp();
Page(PAGE({
    behaviors: [storeBindingsBehavior],
    storeBindings: {
        store,
        fields: {
            needRefresh: 'vehicle.needRefresh'
        },
        actions: {
            updateNeedRefresh: 'vehicle.updateNeedRefresh'
        }
    },
    data: {
        list: [],
        apiField: {
            module: "memberAccount",
            api: "PriceList",
            data: "list",
            search: {},
        },
        tabIndex: 0,
        selectMoneyIndex: 0,
        money: 0,
        phone: null
    },
    onLoad() {
        this.fetchData();
    },
    onShow() {
        if (this.data.needRefresh) {
            this.updateNeedRefresh(false);
            this.fetchData();
        }
    },
    onPullDownRefresh() {
        this.fetchData();
        wx.stopPullDownRefresh();
    },
    async fetchData() {
        this.handleFetchData({
            complete: (data) => {
                this.calc()
            }
        }, "apiField", true);
    },
    handleMoneyItemChange(e) {
        var index = e.currentTarget.dataset.index
        this.setData({
            selectMoneyIndex: index
        })
        this.calc()
    },
    handleTabChange(e) {
        var index = e.currentTarget.dataset.index
        this.setData({
            tabIndex: index
        })
    },
    calc() {
        this.setData({
            money: this.data.list[this.data.selectMoneyIndex].Money
        })
    },
    inputChange(e) {
        this.setData({
            phone: e.detail.value
        })
    },
    async handlePay() {
        try {
            const request = {
                Money: this.data.money,
                RechargeItemId: this.data.list[this.data.selectMoneyIndex].Id
            }

            var method = 'Recharge'
            if (this.data.tabIndex == 1) {
                method = 'HelpRecharge'
                request.Phone = this.data.phone
            }

            const res = await wx.$Api.memberRecharge[method]({ data: request })
            const prePayRes = await wx.$Api.payment.PrePay({ data: { tradeNo: res.Data } })
            console.log('支付参数', prePayRes)
            var _this = this
            wx.$Fun.wxPay(prePayRes.Data).then(resp => {
                _this.updateNeedRefresh(true)
                wx.$Fun.toPage({
                    page: 'memberAccount',
                    params: {
                        pageType: 'redirectTo'
                    }
                })
                wx.$Fun.showToast('充值成功')
            }).catch(e => {
                console.log(e)
            })
        }
        catch (e) {
            wx.$Fun.showToast(e.Message)
        }
    },

}));